﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using TransportAbstractionLibrary;
using TransportAbstractionLibrary.TCP;
using TransportAbstractionLibrary.Common.BufferPool;
using TransportAbstractionLibrary.Layer;

namespace Test
{
    class Program
    {

        private static bool CompareFiles(string file1, string file2)
        {
            FileInfo fileInfo1 = new FileInfo(file1);
            FileInfo fileInfo2 = new FileInfo(file2);

            if (fileInfo1.Length == fileInfo2.Length)
            {
                //Read the bytes from both files.
                byte[] bytes1 = File.ReadAllBytes(file1);
                byte[] bytes2 = File.ReadAllBytes(file2);

                //Make sure the arrays are of equal length.
                if (bytes1.Length != bytes2.Length)
                {
                    return false;
                }

                //Compare each byte.
                for (int i = 0; i <= bytes1.Length - 1; i++)
                {
                    if (bytes1[i] != bytes2[i])
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        static void Main(string[] args)
        {
            Console.Write("Enter filepath: ");
            string filePath = Console.ReadLine();
            if (File.Exists(filePath))
            {
                //Console.WriteLine(CompareFiles(filePath, @"c:\temp\dump.bin"));
                //yay we found the file lets open it and send it!
                //lets create the socket and connect
                Console.Write("Enter host: ");
                string host = Console.ReadLine();
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(host, 123);
                int bufferSize = 1024 * 64;
                BufferManager manager = new BufferManager(bufferSize * 2, 20);
                TCPProtocolBase protoclBase = new TCPProtocolBase(socket, manager);
                PackageLayer packageLayer = new PackageLayer(bufferSize);
                CompressionLayer compressionLayer = new CompressionLayer();
                FileSender fileSender = new FileSender(filePath, bufferSize / 2);
                protoclBase.Bind(packageLayer);
                packageLayer.Bind(compressionLayer);
                compressionLayer.Bind(fileSender);
                protoclBase.Initialize();
            }
            else
            {
                Console.WriteLine("Error file does not exist");
            }
            Console.ReadLine();
        }
    }
}
